// const baseUrl = 'http://192.168.0.122:8081/beedao-applet';//王恒本地地址
// const baseUrl = 'http://192.168.0.110:8081/beedao-applet';//吕凯本地地址
// const baseUrl = 'http://ent.beedao.tech/beedao-applet';
const baseUrl = 'https://www.beedao.tech/beedao-applet';
const mapVxKey = "81e6635bf71b13a1355162ba56542dee";

const httpRequest = (opts, data) => {
	let way = opts.way;
	let httpDefaultOpts = {
		url: baseUrl + '/' + opts.url, 
		method: opts.method,
		header: way ? {
			'X-Requested-With': 'XMLHttpRequest',
			"Accept": "application/json",
			"Content-Type": "application/json;charset=UTF-8"
		} : {
			'X-Requested-With': 'XMLHttpRequest',
			'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
		},
		dataType: 'jsonp',
	}
	let promise = new Promise(function(resolve) {
		uni.request(httpDefaultOpts).then(
			(res) => {
				var data = JSON.parse(res[1].data);
				if (data.code == "1000" || data.code == "1001") {
					// resolve(data)
					// uni.reLaunch({
					// 	url: '/pages/login/login'
					// })
					uni.showModal({
						title: '提示',
						content: data.message,
						confirmText: '去登录',
						success: function(res) {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/login/login'
								});
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				} else {
					resolve(data)
				}	
			}
		).catch(
			(response) => {
				console.log(response);  
				 
			}
		)
	})
	return promise
};
let count = 0;
//带Token请求
const httpTokenRequest = (opts, data) => {
	let token = "";
	if (uni.getStorageSync('token')) {
		token = uni.getStorageSync('token');
	}

	//此token是登录成功后后台返回保存在storage中的
	let way = opts.way;
	let httpDefaultOpts = {
		url: baseUrl + '/' + opts.url,
		data: data,
		method: opts.method,
		header: way ? {
			'token': token,
			'X-Requested-With': 'XMLHttpRequest',
			"Accept": "application/json",
			"Content-Type": "application/json;charset=UTF-8"
		} : {
			'token': token,
			'X-Requested-With': 'XMLHttpRequest',
			'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
		},
		dataType: 'jsonp',
	}
	let promise = new Promise(function(resolve) {
			uni.request(httpDefaultOpts).then(
				(res) => {
					var data = JSON.parse(res[1].data);
					if (data.code == "1000" || data.code == "1001") {
						resolve(data)
						if(count == 0){
							count=1;
							uni.clearStorageSync('token');
							uni.showModal({
								title: '提示',
								content: data.message,
								confirmText: '去登录',
								success: function(res) {
									if (res.confirm) {
										count = 0;
										uni.navigateTo({
											url: '/pages/login/login'
										});
									} else if (res.cancel) {
										count = 0;
										console.log('用户点击取消');
									}
								}
							});
						}
						
				} else {
					resolve(data)
				}

			}
		).catch(
			(response) => {
				// resolve(response)
				console.log(response);
			}
		)
	})
return promise
};

export default {
	baseUrl,
	httpRequest,
	httpTokenRequest,
	mapVxKey
}
